package com.wangtao.firstapp.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.squareup.picasso.Picasso;
import com.wangtao.firstapp.R;
import com.wangtao.firstapp.modles.UserInfoData;
import com.wangtao.firstapp.utils.HttpUtils;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * 用户个人详情界面
 * Created by lanouhn on 16/9/2.
 */
public class UserInfoActivity extends AppCompatActivity {
    private ImageView iv_userinfo_title_back;// 页面左上角标题栏返回按钮
    private TextView tv_userinfo_nickname;// 用户的昵称
    private TextView tv_personal_sign;// 用户的个性签名
    private CircleImageView civ_personal_header;// 用户的头像
    private TextView tv_personal_articlelike_count;// 用户的喜欢数
    private TextView tv_personal_follows_count;// 用户的关注数
    private TextView tv_personal_fans_count;// 用户的粉丝数
    private Button btn_personal_follows, btn_personal_follows_checked;// 关注按钮
    private UserInfoData userInfoData;// 用户个人信息页面实体类
    private TabLayout tablayout_personal;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_userinfo);
        initView();
        initData();
        initEvent();
    }

    private void initView() {
        iv_userinfo_title_back = (ImageView) findViewById(R.id.iv_userinfo_title_back);// 页面左上角标题栏返回按钮
        tv_userinfo_nickname = (TextView) findViewById(R.id.tv_userinfo_nickname);// 用户的昵称
        tv_personal_sign = (TextView) findViewById(R.id.tv_personal_sign);// 用户的个性签名
        civ_personal_header = (CircleImageView) findViewById(R.id.civ_personal_header);// 用户的头像
        tv_personal_articlelike_count = (TextView) findViewById(R.id.tv_personal_articlelike_count);// 用户的喜欢数
        tv_personal_follows_count = (TextView) findViewById(R.id.tv_personal_follows_count);// 用户的关注数
        tv_personal_fans_count = (TextView) findViewById(R.id.tv_personal_fans_count);// 用户的粉丝数
        btn_personal_follows = (Button) findViewById(R.id.btn_personal_follows);// 关注按钮，未关注时的状态
        btn_personal_follows_checked = (Button) findViewById(R.id.btn_personal_follows_checked);// 关注按钮，已关注时的状态
        tablayout_personal = (TabLayout) findViewById(R.id.tablayout_personal);
    }

    private void setTabLayout() {

    }

    private void initData() {
        Intent intent = getIntent();
        String userid = intent.getStringExtra("userid");
        String url = "http://open4.bantangapp.com/users/center/userInfo";
        String param = "app_id=com.jzyd.BanTang&client_id=bt_app_android&client_secret=ffcda7a1c4ff338e05c42e7972ba7b8d&track_user_id=2559956&oauth_token=6beffa8c92276d19d3c23acb596024e6&track_deviceid=866962021116243&track_device_info=m1+note&channel_name=sougou&app_installtime=1471923362&app_versions=5.8.8&os_versions=5.1&screensize=1080&v=19&visit_user_id=" + userid;

        try {
            HttpUtils.doPostAsyn(url, param, new HttpUtils.CallBack() {
                @Override
                public void onRequestComplete(String result) {
                    Gson gson = new Gson();
                    userInfoData = gson.fromJson(result, UserInfoData.class);
                    if (userInfoData != null) {
                        handler.sendEmptyMessage(1);
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void initEvent() {
        iv_userinfo_title_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    break;
                case 1:
                    tv_userinfo_nickname.setText(userInfoData.getData().getNickname());// 用户的昵称
                    tv_personal_sign.setText(userInfoData.getData().getUser_sign());// 用户的个性签名
                    Picasso.with(UserInfoActivity.this).load(userInfoData.getData().getAvatar()).into(civ_personal_header);// 用户的头像
                    tv_personal_articlelike_count.setText(userInfoData.getData().getArticle_like());// 用户的喜欢数
                    if (!"".equals(userInfoData.getData().getAttentions())) {
                        tv_personal_follows_count.setText(userInfoData.getData().getAttentions());// 用户的关注数
                    } else {
                        tv_personal_follows_count.setText("0");// 用户没有关注任何人时
                    }
                    tv_personal_fans_count.setText(userInfoData.getData().getFans());// 用户的粉丝数
                    break;
            }
        }
    };

}
